package net.mhelvens.deltaprofiles;

import android.content.ClipData;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class RuleEditConditionFragment extends Fragment {
    @SuppressWarnings("unused") private static final String TAG = RuleEditConditionFragment.class.getSimpleName();

	private Rule mRule;
	
	private ImageView formulaBackground;
	
	private TextView logicalAnd;
	private TextView logicalOr;
	private TextView logicalNot;
	
	// temporary
	private ImageView dummyDropTarget;
	
	public void collectViews() {
		formulaBackground = (ImageView) getActivity().findViewById(R.id.formula_background);
		
		logicalAnd = (TextView) getActivity().findViewById(R.id.logical_and);
		logicalOr  = (TextView) getActivity().findViewById(R.id.logical_or );
		logicalNot = (TextView) getActivity().findViewById(R.id.logical_not);
		
		// temporary:
		dummyDropTarget = (ImageView) getActivity().findViewById(R.id.dummy_drop_target);
	}
	
	public void setupViews() {
		logicalAnd.setOnLongClickListener(new ConditionDragListener(ClipData.newRawUri("Logical 'and'", LogicalAndCondition.uri())));
		logicalOr .setOnLongClickListener(new ConditionDragListener(ClipData.newRawUri("Logical 'or'" , LogicalOrCondition .uri())));
		logicalNot.setOnLongClickListener(new ConditionDragListener(ClipData.newRawUri("Logical 'not'", LogicalNotCondition.uri())));
		
		formulaBackground.setOnDragListener(new View.OnDragListener() {
			public boolean onDrag(View v, DragEvent event) {
				switch(event.getAction()) {
					case DragEvent.ACTION_DRAG_STARTED: {
						formulaBackground.setImageDrawable(getResources().getDrawable(R.drawable.green_indicator));
						return true;
					}
					case DragEvent.ACTION_DRAG_ENDED: {
						formulaBackground.setImageDrawable(null);
						return true;
					}
				}
				
				return false;
			}
		});
		
		dummyDropTarget.setOnDragListener(new View.OnDragListener() {
			public boolean onDrag(View v, DragEvent event) {
				ImageView iv = (ImageView) v;
				switch(event.getAction()) {
					case DragEvent.ACTION_DRAG_STARTED: {
						return true;
					}
					case DragEvent.ACTION_DRAG_ENTERED: {
						iv.setImageDrawable(getResources().getDrawable(R.drawable.drop_target_open));
						return true;
					}
					case DragEvent.ACTION_DRAG_EXITED: {
						iv.setImageDrawable(getResources().getDrawable(R.drawable.drop_target_closed));
						return true;
					}
					case DragEvent.ACTION_DROP: {
						v.setVisibility(View.GONE);
						return true;
					}
				}
				
				return false;
			}
		});
	}
	
	public void refreshViews() {
		// TODO
	}
	
	RuleEditConditionFragment(Rule rule) {
		mRule = rule;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.rule_edit_condition, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		
		collectViews();
		setupViews();
		refreshViews();
	}
	

	private class ConditionDragListener implements View.OnLongClickListener {
		private ClipData mClipData;
		
		private ConditionDragListener(ClipData clipData) {
			mClipData = clipData;
		}
		
		public boolean onLongClick(View v) {
			DragShadowBuilder dragShadowBuilder = new DragShadowBuilder(v);
			v.startDrag(mClipData, dragShadowBuilder, null, 0);
			return true;
		}
	}
	
}
